package cn.eric.framework.ajax;

import cn.eric.framework.utils.general.merge.EMergePatch;
import com.google.gson.annotations.SerializedName;

/**
 * 用于封装分页信息
 */
public class EricContext implements EMergePatch{
    @SerializedName("page_total")
    private int totalPage;
    @SerializedName("page_num")
    private int currentPage;
    @SerializedName("page_size")
    private int rowPerPage;
    @SerializedName("total_row")
    private long totalRow;

    public static EricContext generateByTotalRows(long totalRows, int currentPage, int rowPerPage) {
        EricContext tmp = new EricContext(0, currentPage, rowPerPage, totalRows);
        tmp.setTotalPageByTotalRows(totalRows);
        return tmp;
    }

    public EricContext() {
    }

    public EricContext(int totalPage, int currentPage, int rowPerPage, long totalRow) {
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.rowPerPage = rowPerPage;
        this.totalRow = totalRow;
    }

    public void setTotalPageByTotalRows(long totalRows) {
        totalPage = (int) (totalRows % rowPerPage == 0 ? totalRows / rowPerPage : totalRows / rowPerPage + 1);
    }

    @Override
    public String toString() {
        return "EricContext{" +
                "totalPage=" + totalPage +
                ", currentPage=" + currentPage +
                ", rowPerPage=" + rowPerPage +
                ", totalRow=" + totalRow +
                '}';
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getRowPerPage() {
        return rowPerPage;
    }

    public void setRowPerPage(int rowPerPage) {
        this.rowPerPage = rowPerPage;
    }

    public long getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(long totalRow) {
        this.totalRow = totalRow;
    }

    @Override
    public Class getTargetClass() {
        return AjaxResponse.class;
    }
}
